package com.taiyi.renaissancemall.ui.mine.collect;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.utils.DensityUtil;
import com.liaoinstan.springview.widget.SpringView;
import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.MyDecoration;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.bean.CollectBean;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//商品收藏列表、商品浏览列表
public class MyCollect2Activity extends BaseActivity<MyCollectView, MyCollectPresent> implements MyCollectView, SpringView.OnFreshListener {

    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightTitleTv)
    TextView rightTitleTv;
    @BindView(R.id.topPanel)
    TextView topPanel;
    @BindView(R.id.recyclerView)
    SwipeMenuRecyclerView recyclerView;
    @BindView(R.id.springView)
    SpringView springView;
    @BindView(R.id.top)
    ConstraintLayout top;
    @BindView(R.id.noDataIv)
    ImageView noDataIv;
    private int page = 1;
    private MyCollectAdapter myCollectAdapter;
    private List<CollectBean.CollectionlistBean> data = new ArrayList<>();
    private String cType = "1";
    private SwipeMenuItem deleteItem;
    private MyDecoration itemDecoration;
    private List<CollectBean.CollectionlistBean> comlist;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_collect;
    }

    @Override
    public MyCollectPresent initPresenter() {
        return new MyCollectPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        cType = getIntent().getStringExtra("com_type");

        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));
        springView.setGive(SpringView.Give.BOTH);
        springView.setType(SpringView.Type.FOLLOW);
        springView.setListener(this);

        if ("1".equals(cType)) {
            SwipeMenuCreator swipeMenuCreator =
                    new SwipeMenuCreator() {
                        @Override
                        public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
                            deleteItem = new SwipeMenuItem(MyCollect2Activity.this);
                            deleteItem.setText(R.string.delete)
                                    .setBackgroundColor(getResources().getColor(R.color.red))
                                    .setTextColor(Color.WHITE) // 文字颜色。
                                    .setTextSize(15) // 文字大小。
                                    .setWidth(240)
                                    .setHeight(DensityUtil.dip2px(MyCollect2Activity.this, 228.5f));
                            swipeRightMenu.addMenuItem(deleteItem);
                        }
                    };

            // 设置监听器。
            // 设置监听器。
            recyclerView.setSwipeMenuCreator(swipeMenuCreator);
            SwipeMenuItemClickListener swipeMenuItemClickListener = new SwipeMenuItemClickListener() {
                @Override
                public void onItemClick(SwipeMenuBridge menuBridge) {
                    menuBridge.closeMenu();
                    int menuPosition = menuBridge.getAdapterPosition();
                    showProgress();
                    presenter.deleteAddress(data.get(menuPosition).collection_id);
                }
            };
            recyclerView.setSwipeMenuItemClickListener(swipeMenuItemClickListener);

            itemDecoration = new MyDecoration();
            itemDecoration.setDividerHeight(dp2px(10));

            recyclerView.addItemDecoration(itemDecoration);
        }

        setAdapter(data);

    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).statusBarDarkFont(false, 1.0f).init();
    }


    @Override
    protected void initData() {

        if ("1".equals(cType)) {
            titleTv.setText(getString(R.string.myCollection));
        } else if ("2".equals(cType)) {
            titleTv.setText(getString(R.string.browsingHistory));
        }
        showProgress();
        //展示
        presenter.getCollect(page + "", cType);
    }

    @Override
    public void initEvent() {
    }

    @Override
    public void CollectSuccess(CollectBean collectBean) {
        hideProgress();
        springView.onFinishFreshAndLoad();
        comlist = collectBean.collectionlist;
        if (page == 1) {
            data.clear();
            if (comlist == null || comlist.size() == 0) {
                noDataIv.setVisibility(View.VISIBLE);
            } else {
                noDataIv.setVisibility(View.GONE);
            }
        }
        data.addAll(comlist);
        if (myCollectAdapter != null) {
            myCollectAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void CollectFailure(String msg) {
        hideProgress();
        ToastUtils.showLongToast(msg);
    }

    @Override
    public void deleteSuccess() {
        page = 1;
        //展示
        presenter.getCollect(page + "", cType);
    }

    @Override
    public void deleteFailure(String msg) {
        ToastUtils.showShortToast(msg);
    }

    private void setAdapter(List<CollectBean.CollectionlistBean> classificationlist) {
        myCollectAdapter = new MyCollectAdapter(R.layout.search_result_item_layout, classificationlist);
        recyclerView.setLayoutManager(new LinearLayoutManager(MyCollect2Activity.this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(myCollectAdapter);
    }

    @Override
    public void onRefresh() {
        page = 1;
        presenter.getCollect(page + "", cType);
    }

    @Override
    public void onLoadmore() {
        page++;
        presenter.getCollect(page + "", cType);
    }

    @OnClick({R.id.backIv})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.backIv) {
            finish();
        }
    }

    public float dp2px(int dpValue) {
        return getResources().getDisplayMetrics().density * dpValue + 0.5f;
    }

}
